package com.hx.er.action.manager.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErManagerAction;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.detail.CityDetailBean;
import com.hx.er.bean.detail.DistrictDetailBean;
import com.hx.er.bean.detail.StreetDetailBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErProperties;
import com.hx.er.service.manager.IManagerRestService;

public class ManagerServiceEditAction extends ErManagerAction {
	// 序列化ID
	private static final long serialVersionUID = 4370191075271103201L;

	private List<CityDetailBean> cityDetailList;
	private transient List<DistrictDetailBean> districtDetailList;
	private transient List<StreetDetailBean> streetDetailList;

	private RestraurantBean restBean;

	// 地图引擎
	private String mapEngine = ErProperties.getProperty(Constants.PROPERTY_MAP_ENGINE);

	// ajax参数
	private String cityIdAjax;
	private String districtIdAjax;

	@Autowired
	private IManagerRestService managerRestService;

	public String init() {
		this.initPage();
		return SUCCESS;
	}

	public String loadCityInfo() {
		this.initList(this.cityIdAjax, null);
		return SUCCESS;
	}

	public String loadDistrictInfo() {
		this.initList(null, this.districtIdAjax);
		return SUCCESS;
	}

	public String save() {
		// 设置餐厅属性
		this.restBean.setRestId(super.restId);
		this.restBean.setUpdateUserId(super.userInfoLogin.getUserId());

		int count = this.managerRestService.updateRestAddr(this.restBean);
		if (count == 0) {
			this.addActionError(super.getText(EDIT_FAILURE_MSG));
			return INPUT;
		} else {
			this.addActionMessage(super.getText(EDIT_SUCCESS_MSG));
		}

		this.initPage();
		return SUCCESS;
	}

	private void initPage() {
		// 查询餐馆属性
		this.restBean = this.managerRestService.getRestById(super.restId);
		this.initList(String.valueOf(this.restBean.getCityId()), String.valueOf(this.restBean.getDistrictId()));
	}

	private void initList(String cityId, String districtId) {
		// 查询列表
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("countryId", ErProperties.getProperty(Constants.PROPERTY_DEFAULT_COUNTRY));
		conditions.put("cityId", cityId);
		conditions.put("districtId", districtId);
		conditions.put("statusId", Constants.STATUS_VALUE9);

		this.cityDetailList = this.managerRestService.getCityDetailList(conditions);
		this.districtDetailList = this.managerRestService.getDistrictDetailList(conditions);
		this.streetDetailList = this.managerRestService.getStreetDetailList(conditions);
	}

	public List<CityDetailBean> getCityDetailList() {
		return cityDetailList;
	}

	public List<DistrictDetailBean> getDistrictDetailList() {
		return districtDetailList;
	}

	public List<StreetDetailBean> getStreetDetailList() {
		return streetDetailList;
	}

	public RestraurantBean getRestBean() {
		return restBean;
	}

	public void setRestBean(RestraurantBean restBean) {
		this.restBean = restBean;
	}

	public String getCityIdAjax() {
		return cityIdAjax;
	}

	public void setCityIdAjax(String cityIdAjax) {
		this.cityIdAjax = cityIdAjax;
	}

	public String getDistrictIdAjax() {
		return districtIdAjax;
	}

	public void setDistrictIdAjax(String districtIdAjax) {
		this.districtIdAjax = districtIdAjax;
	}

	public String getMapEngine() {
		return mapEngine;
	}

}
